package com.carpedil.score.service.impl;

import com.carpedil.dto.ScoreDto;
import com.carpedil.dto.UserDto;
import com.carpedil.jwt.JwtUtil;
import com.carpedil.score.entity.Score;
import com.carpedil.score.mapper.ScoreMapper;
import com.carpedil.score.service.ScoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/** @author Administrator */
@Service
public class ScoreServiceImpl implements ScoreService {
  @Autowired ScoreMapper scoreMapper;

  @Override
  public List<ScoreDto> getScoreList(String token) {
    List<Score> scores = null;
    // 解析token
    UserDto userDto = JwtUtil.verifierJwt(token);
    switch (String.valueOf(userDto.getPerm())) {
      case "1":
        scores = scoreMapper.getScores(0, 5);
        break;
      case "2":
        scores = scoreMapper.getAllScores();
        break;
      default:
        break;
    }
    List<ScoreDto> scoreDtos = new ArrayList<>();
    assert scores != null;
    for (Score score : scores) {
      ScoreDto scoreDto = new ScoreDto();
      try {
        BeanUtils.copyProperties(score, scoreDto);

        scoreDtos.add(scoreDto);
      } catch (BeansException e) {
        e.printStackTrace();
      }
    }
    return scoreDtos;
  }
}
